package unreleased.geppetto.cat.programs;


import geppetto.cat.alignments.AlignmentEvaluator;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.alignments.AlignmentEvaluator.Evaluation;
import geppetto.cat.corpus.BilingualCorpus;
import geppetto.cat.models.HMM;
import geppetto.cat.models.M1;

import java.io.IOException;
import java.util.ArrayList;



public class TestSmoothingMHMM {
	private String corpusDescription;

	private int size, maxSentenceSize;

	BilingualCorpus corpus;

	public void initCorpus(String args[]) throws IOException {
		corpusDescription = args[0];
		size = Integer.parseInt(args[1]);
		maxSentenceSize = Integer.parseInt(args[2]);
		System.out.println("Corpus " + corpusDescription);
		System.out.println("Corpus Size " + size);
		System.out.println("Max sentence size " + maxSentenceSize);
		System.out.println("---");
		System.out.println("Loading Corpus");
		corpus = BilingualCorpus.getCorpusFromFileDescription(
				corpusDescription, size, maxSentenceSize);

	}

	public Evaluation runModel(int nrIterations, double smooth) {
		M1 m1 = new M1(corpus, smooth);
		m1.train(nrIterations,false,"");
		HMM hmm = new HMM(corpus, m1._tb);
		hmm.train(nrIterations,false,"");
		AlignmentsSet alignments = hmm
				.viterbiAlignments(BilingualCorpus.TEST_CORPUS);
		Evaluation eval1 = AlignmentEvaluator.evaluate(alignments, corpus
				.getGold());
		System.out.println("Smoothing =" + smooth);
		System.out.println(eval1.toString());
		return eval1;
	}

	public static void main(String[] args) throws IOException {
		TestSmoothingMHMM test = new TestSmoothingMHMM();
		test.initCorpus(args);
		ArrayList<Evaluation> evals = new ArrayList<Evaluation>();
		ArrayList<Double> vals = new ArrayList<Double>();
		for (double i = 0.01; i > 0.0000001; i = i * 0.5) {
			evals.add(test.runModel(5, i));
			vals.add(i);
		}
		evals.add(test.runModel(5, 0));
		vals.add(0.0);
		System.out.println(vals.size());
		for (int i = vals.size() - 1; i > -1; i--) {
			System.out.println(i);
			Evaluation eval = evals.get(i);
			System.out.println(vals.get(i) + "\t\t" + eval.getPrecision()
					+ "\t" + eval.getRecall() + "\t" + eval.getAER());
		}
	}
}
